package Proj1;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.Buffer;

/**
 * Servlet implementation class ServletLogin
 */
public class ServletLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	String nome = "";

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();		 
		 PrintWriter out = resp.getWriter(); 
		 //out.print("teste");
		 
	     String login = req.getParameter("login");
	     System.out.println(login);
	     String senha = req.getParameter("senha");
	     
	     
	    	 session.setAttribute("login", login);
	    	 session.setAttribute(req.getParameter("senha"), senha);
	    	 Date data = new Date(session.getLastAccessedTime());
			 
			 //out.print("Bem vindo <br>"+ (String)session.getAttribute("login"));
			 
			 FileOutputStream fs = new FileOutputStream("hist.txt", true);
			 BufferedOutputStream wr = new BufferedOutputStream(fs);
			 String res = "Usuario: "+session.getValue("login")+ "\n Sessao: "+session.getId()+"  "+data.toLocaleString()+"\n\n";
			 wr.write(res.getBytes());
			 wr.close();
			 
			 out.println("<html><body> Usuario da sessao: "+ (String)session.getAttribute("login")+ "<a href='Index.jsp'> continuar </a> <br> <br> <br> </body></html>");
			 
			 
			 
		 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		

	}

}
